#include <messageDialog.h>

namespace View {

  gboolean dialogDeleteEvent(GtkWidget *widget,
			     GdkEvent  *event,
			     gpointer   user_data)
  {
    MessageDialog *dialog = static_cast<MessageDialog*>(user_data);
    dialog->Hide();
    return TRUE;
  }

  gboolean dialogButtonOnClicked(GtkButton *button,
				 gpointer   user_data)
  {
    MessageDialog *dialog = static_cast<MessageDialog*>(user_data);
    dialog->Hide();
    return TRUE;
  }

  MessageDialog *MessageDialog::instance = NULL;

  MessageDialog *MessageDialog::GetMessageDialog()
  {
    if(NULL == MessageDialog::instance)
      MessageDialog::instance = new MessageDialog();
    return MessageDialog::instance;
  }

  MessageDialog::MessageDialog()
    :window(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
     canvas(goo_canvas_new()),
     root(goo_canvas_get_root_item((GooCanvas*)canvas)),
     scrollWindow(gtk_scrolled_window_new(NULL,NULL)),
     buffer(gtk_text_buffer_new(NULL)),
     confirmButton(gtk_button_new())
  {
    ViewConf *conf = ViewConf::GetViewConf();
    gdouble dialogLength(conf->GetDoubleData("dialogLength")),
      dialogHeigth(conf->GetDoubleData("dialogHeigth")),
      dialogFontSize(conf->GetDoubleData("dialogFontSize"));
    std::string dialogConfirm(conf->GetStringData("dialogConfirm"));

    gtk_text_buffer_get_start_iter (buffer, &iter);
    textView = gtk_text_view_new_with_buffer(buffer);
    gtk_text_view_set_editable((GtkTextView *)textView,FALSE);
    gtk_text_view_set_wrap_mode((GtkTextView *)textView,GTK_WRAP_WORD);
    gtk_text_view_set_cursor_visible((GtkTextView *)textView,FALSE);

    gtk_widget_set_size_request(window, dialogLength , dialogHeigth);
    gtk_window_set_position((GtkWindow*)window,GTK_WIN_POS_CENTER);
    gtk_container_add((GtkContainer*)window,canvas);
    gtk_container_add(GTK_CONTAINER(scrollWindow),textView);

    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollWindow),GTK_SHADOW_ETCHED_IN);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollWindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

    gtk_button_set_label((GtkButton*)confirmButton,dialogConfirm.c_str());
    
    goo_canvas_widget_new(root,scrollWindow,0,0,dialogLength,dialogHeigth - 40,NULL);
    goo_canvas_widget_new(root,confirmButton,0,dialogHeigth - 40,100,40,NULL);

    g_signal_connect(G_OBJECT(window),"delete_event",
		     G_CALLBACK(dialogDeleteEvent),this);
    g_signal_connect(G_OBJECT(confirmButton),"clicked",
		     G_CALLBACK(dialogButtonOnClicked),this);

    gtk_text_buffer_create_tag (buffer, "normal",
				"size", (int)dialogFontSize*PANGO_SCALE,
				"foreground", "#1D597D",
				//  "style", PANGO_STYLE_ITALIC,
				//  "underline",PANGO_UNDERLINE_SINGLE,
				NULL);
  }

  MessageDialog::~MessageDialog()
  {

  }

  bool MessageDialog::ClearText(){
    gtk_text_buffer_set_text(buffer,"",0);
    gtk_text_buffer_get_start_iter (buffer, &iter);
    return true;
  }

  bool MessageDialog::SetText(std::string newText)
  {
    ClearText();
    gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
                                              newText.c_str(),-1,"normal",NULL);
    return true;
  }

  bool MessageDialog::SetTitle(std::string newTitle)
  {
    gtk_window_set_title((GtkWindow *)window,newTitle.c_str());
    return true;
  }

  bool MessageDialog::Show()
  {
    gtk_widget_show_all(window);
    return true;
  }

  bool MessageDialog::Hide()
  {
    gtk_widget_hide_all(window);
    return true;
  }

}
